<!-- 门店列表 -->
<template>
	<view class="content">
		<u-swipe-action :show="item.show" :index="index" v-for="(item, index) in list" :key="index" @click="goedit(item.id)"
			@open="open" :options="options" btn-width="74">
			<view class="item ">
				<view class="">
					<image :src="item.shop_image"></image>
				</view>
				<!-- 此层wrap在此为必写的，否则可能会出现标题定位错误 -->
				<view class="title-wrap">
					<view class="item-name">
						<text class="title">{{item.shop_name}}</text>
            <block v-if="item.check_state!='2'">
              <text v-if="item.check_state == '1'" class="item-state">审核中</text>
              <text v-if="item.check_state == '3'" class="item-state">审核不通过</text>
            </block>
            <block v-else>
              <text v-if="item.open_state == 0" class="item-state">休息</text>
              <text v-else class="item-state">正常营业</text>
            </block>
					</view>
					<view class="item-price">
						<text>结算折扣：{{item.discount}}</text>
					</view>
					<view class="item-tool">
            <text class="tool-item u-m-r-16" v-if="item.check_state=='2'"  @click="gotoPoster(item.store_shop_id)">优惠券海报</text>
						 <text class="tool-item u-m-r-16" v-if="item.check_state=='2'"   @click="sendHongba(item.store_shop_id)">申请发米包</text>
						<text class="tool-item" @click="gotoEdit(item.store_shop_id)">编辑</text>
						<!-- <text class="tool-item">菜品</text> -->
					</view>
				</view>
			</view>
		</u-swipe-action>

		<!-- 加载更多 -->
		<u-loadmore v-show="!isEmpty" height="80rpx" :status="loadStatus" icon-type="flower" color="#ccc" />

		<view class="foot_box u-flex u-row-between u-col-center"><button class="u-reset-button save-btn"
				@tap="addStore">增加门店</button></view>
	</view>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				isEmpty: false,
				list: [],
				listParams: {
					page: 1
				},
				storeId: uni.getStorageSync('storeId'),
				disabled: false,
				btnWidth: 180,
				show: false,
				options: [{
						text: '设置',
						style: {
							backgroundColor: '#dd524d',
							borderBottom:'24rpx solid #F8F8F8'
						}
					},
					{
						text: '删除',
						style: {
							backgroundColor: '#007aff',
							borderBottom:'24rpx solid #F8F8F8'
						}
					}

				]
			};
		},
		computed: {},
		onShow() {
			this.list = [];
			this.listParams.page = 1;
			this.getStoreAddress();
		},
		onLoad() {
			// this.getStoreAddress();
		},
		// 触底加载更多
		onReachBottom() {
			if (this.listParams.page < this.lastPage) {
				this.listParams.page += 1;
				this.getStoreAddress();
			}
		},
		methods: {
			// 选择门店
			selStore(id) {
				this.storeId = id;
			},
			// 增加门店
			addStore() {
				uni.navigateTo({
					url: 'pages/app/merchant/apply'
				});
			},
			// 编辑门店
			gotoEdit(id) {
				uni.navigateTo({
					url: 'pages/app/merchant/edit?id='+id
				});
			},
      sendHongba(id){
        let that = this;
        let obj = {
          token:uni.getStorageSync('token') ,
          store_shop_id:id
        }
        that.$http_business('store.applyhongbao',obj).then(res => {
          if (res.code == 1) {
            this.$u.toast(res.msg);
          }
        });
      },
			// 获取门店列表
			getStoreAddress() {
				let that = this;
				that.$http_business('store.list',that.listParams).then(res => {
					if (res.code == 1) {
						that.list = [...that.list, ...res.data.data];
						that.isEmpty = !that.list.length;
						that.lastPage = res.data.last_page;
						that.loadStatus = that.listParams.page < res.data.last_page ? 'loadmore' : 'nomore';
					}
				});
			},
			click(index, index1) {
				if (index1 == 1) {
					let params = {
						id: this.list[index].id
					}
					this.$http_business('store.del',params).then(res => {
						if (res.code == 1) {
							this.$u.toast(`删除成功`);
							this.list = [];
							this.listParams.page = 1;
							this.getStoreAddress();
						}
						else{
							this.$u.toast(res.msg);
						}
					});
				} else {
					let temp = this.list[index].open_state == 1 ? 0 : 1
					let params = {
						id: this.list[index].id,
						open_state :temp
					}
					this.$http_business('store.rest',params).then(res => {
						if (res.code == 1) {
							this.$u.toast(`设置成功`);
							this.list[index].show = false;
							this.list[index].open_state = temp
						}
						else{
							this.$u.toast(res.msg);
						}
					});
				}
			},
			// 如果打开一个的时候，不需要关闭其他，则无需实现本方法
			open(index) {
				// 先将正在被操作的swipeAction标记为打开状态，否则由于props的特性限制，
				// 原本为'false'，再次设置为'false'会无效
				this.list[index].show = true;
				this.list.map((val, idx) => {
					if (index != idx) this.list[idx].show = false;
				})
			},
      gotoPoster(id){
        uni.navigateTo({
          url: 'pages/today/poster?id=' + id
        });
      }
		}
	};
</script>

<style lang="scss">
	.content {
		padding-bottom: 130rpx;
		background-color: #F8F8F8;
	}

	.title-wrap {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
	}

	.item {
		display: flex;
		align-items: center;
		box-sizing: border-box;
		padding: 30rpx 40rpx;
		border-bottom: 24rpx solid #F8F8F8;
	}

	image {
		width: 160rpx;
		height: 160rpx;
		display: inline-block;
		margin-right: 24rpx;
		border-radius: 12rpx;
	}

	.item-name {
		display: flex;
		align-items: baseline;
		justify-content: space-between;

		.title {
			width: 70%;
			text-align: left;
			font-size: 28rpx;
			color: #333333;
			margin-top: 20rpx;
			line-height: 42rpx;
		}

		.item-state {
			font-size: 26rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: #FCAB51;
		}
	}

	.item-price {
		font-size: 26rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #FC5351;
		line-height: 36rpx;
	}

	.item-tool {
		// margin-top: 26rpx;
		display: flex;
	}

	.tool-item {
    margin-top: 15rpx;
    padding: 0rpx 30rpx;
    line-height: 52rpx;
    text-align: center;
    border-radius: 26rpx;
    border: 1px solid #D1D2D5;
    font-size: 20rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #666666;
		margin-bottom: 24rpx;
	}

	.tool-item:first-child {
		margin-left: auto;
	}

	.foot_box {
		position: fixed;
		bottom: 0;
		width: 100%;
		background: rgba(255, 255, 255, 1);
		padding: 24rpx 30rpx;

		.save-btn {
			width: 100%;
			line-height: 80rpx;
			background: #FC5351;
			border: 1rpx solid rgba(238, 238, 238, 1);
			font-size: 32rpx;
			font-weight: 500;
			color: rgba(255, 255, 255, 1);
			border-radius: 40rpx;
		}
	}
</style>
